package com.xgkj.diyiketang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.SubSchoolTask;
import com.xgkj.diyiketang.util.ToastUtil;

public class EduExperienceActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout youeryuanLayout;
    private RelativeLayout xiaoxueLayout;
    private RelativeLayout chzhongLayout;
    private RelativeLayout gaozhongLayout;
    private RelativeLayout daxueLayout;
    private RelativeLayout shoushiLayout;
    private RelativeLayout boshiLayout;
    private RelativeLayout workUnitLayout;
    private TextView youeryaun;
    private TextView xiaoxue;
    private TextView chuzhong;
    private TextView gaozhong;
    private TextView daxue;
    private TextView boshi;
    private TextView shuoshi;
    private TextView workUnit;
    private EatShowUserInfo userInfo;
    private TextView saveinfo;
    private int subSchoolTaskId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edu_experience);
        initView();
    }

    private void initView() {
        ((TextView) findViewById(R.id.back)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        saveinfo = (TextView) findViewById(R.id.saveinfo);
        saveinfo.setOnClickListener(this);
        youeryuanLayout = (RelativeLayout) findViewById(R.id.youeryuan_Layout);
        youeryuanLayout.setOnClickListener(this);

        xiaoxueLayout = (RelativeLayout) findViewById(R.id.xiaoxue_layout);
        xiaoxueLayout.setOnClickListener(this);

        chzhongLayout = (RelativeLayout) findViewById(R.id.chuzhong_Layout);
        chzhongLayout.setOnClickListener(this);

        gaozhongLayout = (RelativeLayout) findViewById(R.id.gaozhong_Layout);
        gaozhongLayout.setOnClickListener(this);

        daxueLayout = (RelativeLayout) findViewById(R.id.daxue_Layout);
        daxueLayout.setOnClickListener(this);

        shoushiLayout = (RelativeLayout) findViewById(R.id.shuoshi_Layout);
        shoushiLayout.setOnClickListener(this);

        boshiLayout = (RelativeLayout) findViewById(R.id.boshi_Layout);
        boshiLayout.setOnClickListener(this);

        workUnitLayout = (RelativeLayout) findViewById(R.id.work_unit_Layout);
        workUnitLayout.setOnClickListener(this);


        youeryaun = (TextView) findViewById(R.id.user_youeryuan);
        xiaoxue = (TextView) findViewById(R.id.user_xiaoxue);
        chuzhong = (TextView) findViewById(R.id.user_chuzhong);
        gaozhong = (TextView) findViewById(R.id.user_gaozhong);
        daxue = (TextView) findViewById(R.id.user_daxue);
        boshi = (TextView) findViewById(R.id.user_boshi);
        shuoshi = (TextView) findViewById(R.id.user_shuoshi);
        workUnit = (TextView) findViewById(R.id.user_work_unit);

        Bundle bundle = getIntent().getBundleExtra("school");
        youeryaun.setText(bundle.getString("youeryuan"));
        xiaoxue.setText(bundle.getString("xiaoxue"));
        chuzhong.setText(bundle.getString("chuzhong"));
        gaozhong.setText(bundle.getString("gaozhong"));
        daxue.setText(bundle.getString("daxue"));
        boshi.setText(bundle.getString("shuoshi"));
        shuoshi.setText(bundle.getString("boshi"));
        workUnit.setText(bundle.getString("work"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.youeryuan_Layout:
                Intent intent1 = new Intent(EduExperienceActivity.this, SearchSchoolActivity.class);
                intent1.putExtra("title", "幼儿园");
                intent1.putExtra("type", 01);
                intent1.putExtra("resultcode", 111);
                startActivityForResult(intent1, 111);
                break;
            case R.id.xiaoxue_layout:
                Intent intent2 = new Intent(EduExperienceActivity.this, SearchSchoolActivity.class);
                intent2.putExtra("title", "小学");
                intent2.putExtra("type", 02);
                intent2.putExtra("resultcode", 222);
                startActivityForResult(intent2, 222);
                break;
            case R.id.chuzhong_Layout:
                Intent intent3 = new Intent(EduExperienceActivity.this, SearchSchoolActivity.class);
                intent3.putExtra("title", "初中");
                intent3.putExtra("type", 03);
                intent3.putExtra("resultcode", 333);
                startActivityForResult(intent3, 333);
                break;
            case R.id.gaozhong_Layout:
                Intent intent4 = new Intent(EduExperienceActivity.this, SearchSchoolActivity.class);
                intent4.putExtra("title", "高中");
                intent4.putExtra("type", 04);
                intent4.putExtra("resultcode", 444);
                startActivityForResult(intent4, 444);
                break;
            case R.id.daxue_Layout:
                Intent intent5 = new Intent(EduExperienceActivity.this, SearchSchoolActivity.class);
                intent5.putExtra("title", "大学");
                intent5.putExtra("type", 1);
                intent5.putExtra("resultcode", 555);
                startActivityForResult(intent5, 555);
                break;
            case R.id.shuoshi_Layout:
                Intent intent6 = new Intent(EduExperienceActivity.this, SearchSchoolActivity.class);
                intent6.putExtra("title", "硕士");
                intent6.putExtra("type", 1);
                intent6.putExtra("resultcode", 666);
                startActivityForResult(intent6, 666);
                break;
            case R.id.boshi_Layout:
                Intent intent7 = new Intent(EduExperienceActivity.this, SearchSchoolActivity.class);
                intent7.putExtra("title", "博士");
                intent7.putExtra("type", 1);
                intent7.putExtra("resultcode", 777);
                startActivityForResult(intent7, 777);
                break;
            case R.id.work_unit_Layout:
                Intent intent8 = new Intent(EduExperienceActivity.this, SearchSchoolActivity.class);
                intent8.putExtra("title", "工作单位");
                intent8.putExtra("type", 1);
                intent8.putExtra("resultcode", 888);
                startActivityForResult(intent8, 888);
                break;
            case R.id.saveinfo:
                String schoolALL = "" + youeryaun.getText() + ";" + xiaoxue.getText() + ";" + chuzhong.getText() + ";" + gaozhong.getText() + ";" + daxue.getText() + ";" + shuoshi.getText() + ";" + boshi.getText() + ";" + workUnit.getText();
                SubSchoolTask task = new SubSchoolTask(schoolALL, youeryaun.getText().toString(), xiaoxue.getText().toString(), chuzhong.getText().toString(), gaozhong.getText().toString(), daxue.getText().toString(), shuoshi.getText().toString(), boshi.getText().toString(), workUnit.getText().toString());
                task.addListener(subSchoolListListener);
                subSchoolTaskId = getEatShowApp().getTaskManager().addTask(task);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 111 && data != null) {
            setResult(RESULT_OK, data);
            Bundle bundle = data.getBundleExtra("bundle");
            String school = bundle.getString("school");
            youeryaun.setText(school);
        }
        if (resultCode == 222 && data != null) {
            setResult(RESULT_OK, data);
            Bundle bundle = data.getBundleExtra("bundle");
            String school = bundle.getString("school");
            xiaoxue.setText(school);
        }
        if (resultCode == 333 && data != null) {
            setResult(RESULT_OK, data);
            Bundle bundle = data.getBundleExtra("bundle");
            String school = bundle.getString("school");
            chuzhong.setText(school);
        }
        if (resultCode == 444 && data != null) {
            setResult(RESULT_OK, data);
            Bundle bundle = data.getBundleExtra("bundle");
            String school = bundle.getString("school");
            gaozhong.setText(school);
        }
        if (resultCode == 555 && data != null) {
            setResult(RESULT_OK, data);
            Bundle bundle = data.getBundleExtra("bundle");
            String school = bundle.getString("school");
            daxue.setText(school);
        }
        if (resultCode == 666 && data != null) {
            setResult(RESULT_OK, data);
            Bundle bundle = data.getBundleExtra("bundle");
            String school = bundle.getString("school");
            shuoshi.setText(school);
        }
        if (resultCode == 777 && data != null) {
            setResult(RESULT_OK, data);
            Bundle bundle = data.getBundleExtra("bundle");
            String school = bundle.getString("school");
            boshi.setText(school);
        }
        if (resultCode == 888 && data != null) {
            setResult(RESULT_OK, data);
            Bundle bundle = data.getBundleExtra("bundle");
            String school = bundle.getString("school");
            workUnit.setText(school);
        }
    }

    private TaskEventListener subSchoolListListener = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            finish();
            ToastUtil.showMessage(mContext, "修改完成");
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);

        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

    };

}
